<?php

namespace App\Admin\Metrics\ProjectDesc;

use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use App\Models\Device;
use App\Models\Weikong;

class DeviceCollection extends Line
{
    protected $week = [];
    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        $this->getWeek();

        parent::init();

        $this->chartHeight(350);
        $this->chartColors(array_slice($this->customColors, 0, 2));

        $this->title('设备采集量');
    }

    protected function getWeek(){
        $weekarray=array("日","一","二","三","四","五","六");

        for ($i=6; $i >=0 ; $i--) {
            $dayTime = strtotime("-{$i} day");
            $date = date('Y-m-d H:i:s', $dayTime);
            // $this->week[] = date('l', $dayTime);
            $this->week[] = '周'.$weekarray[date('w', $dayTime)];
        }
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $generator = function ($len, $min = 10, $max = 300) {
            for ($i = 0; $i <= $len; $i++) {
                yield mt_rand($min, $max);
            }
        };

        switch ($request->get('option')) {
            case '365':
                // 卡片内容
                $this->withContent(mt_rand(1000, 5000).'k');
                // 图表数据
                $this->withChart(collect($generator(30))->toArray());
                break;
            case '30':
                // 卡片内容
                $this->withContent(mt_rand(400, 1000).'k');
                // 图表数据
                $this->withChart(collect($generator(30))->toArray());
                break;
            case '28':
                // 卡片内容
                $this->withContent(mt_rand(400, 1000).'k');
                // 图表数据
                $this->withChart(collect($generator(28))->toArray());
                break;
            case '7':
            default:
                // 卡片内容
                $this->withContent('89.2k');
                // 图表数据
                $data = $this->getData();
                $this->chartColors(array_slice($this->customColors, 0, count($data)));
                $this->withChart($data);
        }
    }

    protected function getData(){
        $projectId = session('now_project');
        $devices = Device::where('projectId', $projectId)->where('group', '!=', '')->orderBy('deviceId', 'asc')->get()->toArray();
        $data = [];
        foreach ($devices as $oneDevice) {
            $one = [];
            $one['name'] = $oneDevice['devicename'];
            $parent = Device::find($oneDevice['pid']);
            for ($i=6; $i >= 0; $i--) {
                $start = date('Y-m-d', strtotime("-$i day")).' 00:00:00';
                $end = date('Y-m-d', strtotime("-$i day")).' 23:59:59';
                $one['data'][] = Weikong::where('address', $oneDevice['group'])->where('pname', $parent['gatewayId'])->where('gatewayId', $oneDevice['gatewayId'])->whereBetween('created_at', [$start, $end])->count();
            }
            array_push($data, $one);
        }
        return $data;
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'chart' => [
                'type' => 'line'
            ],
            'series' => $data,
            'xaxis' => [
                'type' => 'category',
                'categories' => $this->week,
                'labels' => [
                    'show' => true
                ],
                'title' => [
                    'text' => '本周'
                ]
            ],
            'yaxis' => [
                'show' => true,
                'showAlways' => true
            ],
            'grid' => [
                'show' => true
            ],
            'fill' => [
                'opacity' => 0.8,
                'type' => 'solid',
            ],
            'markers' => [
                'size' => 1
            ],
            // 'dataLabels' => [
            //     'enabled' => true
            // ]
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h2 class="ml-1 font-lg-1"></h2>
    <span class="mb-0 mr-1 text-80"></span>
</div>
HTML
        );
    }
}
